﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using YArchitech.Electric;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;

namespace YArchitech.MEP.Electric.CableLay
{
	public partial class NewProjectForm : System.Windows.Forms.Form
	{
		public NewProjectForm()
		{
			this.InitializeComponent();
		}

		private void NewProjectForm_Load(object sender, EventArgs e)
		{
			base.Icon = Resource.YArchitechIcon;
			this.InitTabPageProject();
			this.InitTabPageHeaderField();
		}

		private void InitTabPageProject()
		{
			this.txtProjectName.Text = "新工程";
			this.txtProjectPath.Text = Path.Combine(Product.InstallLocation, "UserWork");
			this.txtProjectDescription.Text = string.Empty;
		}

		private void buttonBrowse_Click(object sender, EventArgs e)
		{
			FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
			folderBrowserDialog.SelectedPath = this.txtProjectPath.Text;
			folderBrowserDialog.Description = "请选择工程文件存放路径";
			if (folderBrowserDialog.ShowDialog(this) == DialogResult.OK)
			{
				this.txtProjectPath.Text = folderBrowserDialog.SelectedPath;
			}
		}

		private void InitTabPageHeaderField()
		{
			this.buttonUp.Image = YArchitech.Electric.Resources.GetImage("Up16");
			this.buttonDown.Image = YArchitech.Electric.Resources.GetImage("Next16");
			ToolTip toolTip = new ToolTip();
			toolTip.InitialDelay = 500;
			toolTip.AutoPopDelay = 3000;
			toolTip.ReshowDelay = 50;
			toolTip.ShowAlways = true;
			toolTip.SetToolTip(this.buttonUp, "上移");
			toolTip.SetToolTip(this.buttonDown, "下移");
			this.m_FieldList.Add(new CableListField("电缆编号", "CableNum", "电缆编号", true, true));
			this.m_FieldList.Add(new CableListField("起点设备编号", "StartNum", "起点设备编号", true, true));
			this.m_FieldList.Add(new CableListField("终点设备编号", "EndNum", "终点设备编号", true, true));
			this.m_FieldList.Add(new CableListField("电缆型号", "CableModel", "电缆型号", true, false));
			this.m_FieldList.Add(new CableListField("电缆规格", "CableSpec", "电缆规格", true, false));
			this.m_FieldList.Add(new CableListField("电缆长度", "Length", "电缆长度", true, false));
			this.m_FieldList.Add(new CableListField("敷设路径", "LayPath", "敷设路径", true, false));
			this.m_FieldList.Add(new CableListField("备注", "Remark", "备注", true, false));
			this.listBoxFields.ItemHeight = 16;
			foreach (CableListField item in this.m_FieldList)
			{
				this.listBoxFields.Items.Add(item);
			}
			if (this.listBoxFields.Items.Count > 0)
			{
				this.listBoxFields.SelectedIndex = 0;
			}
		}

		private void listBoxFields_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.BackToFieldItem();
			CableListField cableListField = (CableListField)this.listBoxFields.SelectedItem;
			if (cableListField != null)
			{
				this.txtFieldTitle.Text = cableListField.Title;
				this.txtFieldTitle.Tag = cableListField;
				this.checkBoxForQuery.Checked = cableListField.IsForQuery;
			}
			this.RefreshUpDownButtonState();
		}

		private void buttonUp_Click(object sender, EventArgs e)
		{
			int selectedIndex = this.listBoxFields.SelectedIndex;
			if (selectedIndex == 0)
			{
				return;
			}
			CableListField value = (CableListField)this.listBoxFields.Items[selectedIndex - 1];
			this.listBoxFields.Items[selectedIndex - 1] = this.listBoxFields.Items[selectedIndex];
			this.listBoxFields.Items[selectedIndex] = value;
			this.listBoxFields.SelectedIndex = selectedIndex - 1;
		}

		private void buttonDown_Click(object sender, EventArgs e)
		{
			int selectedIndex = this.listBoxFields.SelectedIndex;
			if (this.listBoxFields.Items.Count - 1 == selectedIndex)
			{
				return;
			}
			CableListField value = (CableListField)this.listBoxFields.Items[selectedIndex + 1];
			this.listBoxFields.Items[selectedIndex + 1] = this.listBoxFields.Items[selectedIndex];
			this.listBoxFields.Items[selectedIndex] = value;
			this.listBoxFields.SelectedIndex = selectedIndex + 1;
		}

		private void BackToFieldItem()
		{
			if (this.txtFieldTitle.Tag != null)
			{
				CableListField cableListField = this.txtFieldTitle.Tag as CableListField;
				cableListField.Title = this.txtFieldTitle.Text;
				cableListField.IsForQuery = this.checkBoxForQuery.Checked;
			}
		}

		private void RefreshUpDownButtonState()
		{
			if (this.listBoxFields.SelectedIndex == 0)
			{
				this.buttonUp.Enabled = false;
				this.buttonDown.Enabled = true;
				return;
			}
			if (this.listBoxFields.Items.Count - 1 == this.listBoxFields.SelectedIndex)
			{
				this.buttonUp.Enabled = true;
				this.buttonDown.Enabled = false;
				return;
			}
			this.buttonUp.Enabled = true;
			this.buttonDown.Enabled = true;
		}

		private void buttonOk_Click(object sender, EventArgs e)
		{
			string text = this.txtProjectName.Text;
			if (string.IsNullOrWhiteSpace(text))
			{
				return;
			}
			string text2 = this.txtProjectPath.Text.TrimEnd(new char[]
			{
				'\\'
			});
			text2 = text2.TrimEnd(new char[]
			{
				'/'
			});
			if (string.IsNullOrWhiteSpace(text2))
			{
				return;
			}
			text += ".hdb";
			this.m_projectFile = Path.Combine(text2, text);
			string sourceFileName = Path.Combine(Product.DataLocation, "ElectricalData", "CableLayProject.hdb");
			if (File.Exists(this.m_projectFile))
			{
				if (MessageBox.Show("此位置已经包含同名文件，是否替换？", "盈建科提示", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk) != DialogResult.Yes)
				{
					return;
				}
				File.Copy(sourceFileName, this.m_projectFile, true);
			}
			else
			{
				File.Copy(sourceFileName, this.m_projectFile);
			}
			this.m_projectDescription = this.txtProjectDescription.Text;
			this.BackToFieldItem();
			this.m_FieldList.Clear();
			for (int i = 0; i < this.listBoxFields.Items.Count; i++)
			{
				this.m_FieldList.Add((CableListField)this.listBoxFields.Items[i]);
			}
			base.Close();
			base.DialogResult = DialogResult.OK;
		}

		public string ProjectFile
		{
			get
			{
				return this.m_projectFile;
			}
		}

		public string ProjectDescription
		{
			get
			{
				return this.m_projectDescription;
			}
		}

		public List<CableListField> HeaderFields
		{
			get
			{
				return this.m_FieldList;
			}
		}

		private string m_projectFile = string.Empty;

		private string m_projectDescription = string.Empty;

		private List<CableListField> m_FieldList = new List<CableListField>();
	}
}
